package com.ifeedme.rocketremote
{
	/**
	 * @author Jim Hayes (www.ifeedme.com) may 2007
	 * The flex prefs object uses sharedObject as it's storage mechanism,
	 *  since we have no local file access under the standard flash player
	 */	
	 
	import flash.net.SharedObject;
	 
	public class PrefsObjectFlex extends PrefsObject implements IPrefsObject
	{
				
		private var sharedObj:SharedObject;
		
		function PrefsObjectFlex():void
		{
			super();			
			sharedObj = SharedObject.getLocal("preferences");
		}
		
		/**
		 * Read all the preferences from local storage, signal that object is ready when done 
		 */		
		override public function readPrefs():void
		{
			readFrontends();
			readKeyMap();
			signalReady();
		}
		
		override protected function readFrontends():void
		{
			///mmmm, saving <frontends></frontends> as xml shared object does not seem to work, it is read out as "". 
			if (sharedObj.data.frontends == null || sharedObj.data.frontends == "")
			{
				firstRun();	
			}
			else
			{
				trace ( " frontends record >>>>>>" + sharedObj.data.frontends + "<<<<<<<<<<<<<<<<<");
				_frontends = sharedObj.data.frontends;
			}			
		}
		
		override protected function readKeyMap():void
		{
			
			_keyMapXml = sharedObj.data.keyMap;
			
			if (_keyMapXml == null)
			{
				_keyMapXml = getDefaultKeyMap();
				writeKeyMap(_keyMapXml);
			}					
		}
		
		override public function writeKeyMap(keyMapXml:XML):void
		{
			sharedObj.data.keyMap = keyMapXml;
     		sharedObj.flush();
		}
		
		override public function writePrefs(str:String):void
		{
			///mmmm, saving <frontends></frontends> as xml shared object does not seem to work, it is read out as "".
			str = '<?xml version="1.0" encoding="utf-8"?>\n'+ str;
			sharedObj.data.frontends = XML(str);			
     		sharedObj.flush();
		}
		
		private function getDefaultKeyMap():XML
		{
			var keyMapXml:XML = <actions>
									<action><button>channelUp</button><cmd>play channel up</cmd></action>
									<action><button>channelDown</button><cmd>play channel down</cmd></action>
									<action><button>tuner</button><cmd>key y</cmd></action>
									<action><button>pause</button><cmd>key p</cmd></action>
									<action><button>right</button><cmd>key right</cmd></action>
									<action><button>left</button><cmd>key left</cmd></action>
									<action><button>up</button><cmd>key up</cmd></action>
									<action><button>down</button><cmd>key down</cmd></action>
									<action><button>volUp</button><cmd>key bracketright</cmd></action>
									<action><button>volDown</button><cmd>key bracketleft</cmd></action>
									<action><button>enter</button><cmd>key enter</cmd></action>
									<action><button>escape</button><cmd>key escape</cmd></action>
									<action><button>record</button><cmd>key return</cmd></action>
									<action><button>skipStart</button><cmd>key q</cmd></action>
									<action><button>skipEnd</button><cmd>key z</cmd></action>
									<action><button>seekForward</button><cmd>key pageup</cmd></action>
									<action><button>seekBackward</button><cmd>key pagedown</cmd></action>
									<action><button>play</button><cmd>play speed 1x</cmd></action>
									<action><button>mute</button><cmd>key f9</cmd></action>
									<action><button>menu</button><cmd>key m</cmd></action>
									<action><button>one</button><cmd>key 1</cmd></action>
									<action><button>two</button><cmd>key 2</cmd></action>
									<action><button>three</button><cmd>key 3</cmd></action>
									<action><button>four</button><cmd>key 4</cmd></action>
									<action><button>five</button><cmd>key 5</cmd></action>
									<action><button>six</button><cmd>key 6</cmd></action>
									<action><button>seven</button><cmd>key 7</cmd></action>
									<action><button>eight</button><cmd>key 8</cmd></action>
									<action><button>nine</button><cmd>key 9</cmd></action>
									<action><button>zero</button><cmd>key 0</cmd></action>	
								</actions>;
							
			return keyMapXml;
		}
		
	}
}